package com.zkingsoft.web.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.common.bean.SysCompany;
import com.zkingsoft.common.dao.SysCompanyDao;
import com.zkingsoft.common.service.SysUsersService;
import com.zkingsoft.web.bean.WebTemplate;
import com.zkingsoft.web.dao.WebTemplateDao;

/**
 * @description 管理员总action
 * @author 姜友瑶
 * @email 935090232@qq.com
 * @date 2016-06-26
 */
@Controller
@RequestMapping(value = "super/jsp")
public class SuperJspEditAction extends BaseAction {
	@Autowired
	private SysUsersService sysUsersService;

	public static final String BEV = "SYSUSERS_BEV";

	@Autowired
	WebTemplateDao webTemplateDao;

	@Autowired
	SysCompanyDao companyDao;
 
	/**
	 * 进入修改界面
	 */
	private String jspPath = "";

	
	/**
	 * 修改jsp文件
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */   
   	@RequestMapping(value =  "/modifyWebTemplate")
	public @ResponseBody AjaxResult modifyWebTemplate(WebTemplate newWebTemplate) throws FileNotFoundException, IOException {
   		File jsp = new File(jspPath);
		if (jsp.isFile()) {
			IOUtils.write(newWebTemplate.getTempContent(), new FileOutputStream(jsp));
		} else {
			throw new GlobleException("源文件路径不存在path={}", jspPath);
		}
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "网页模板");
	}
	
	
	@RequestMapping(value = "/editForm")
	public String editForm(String id) throws Exception {
		WebTemplate webTemplate = webTemplateDao.selectById(id);
		SysCompany company = companyDao.selectById(webTemplate.getCompanyId());
		jspPath = WebUtil.getServletContext().getRealPath("/") + "WEB-INF" + File.separator + "views"
				+ File.separator + "visitor" + File.separator + company.getCode()
				+ File.separator + webTemplate.getTempPath().replaceAll("do/redirect/", "") + ".jsp";
		File jsp = new File(jspPath);
		if (jsp.isFile()) {
			String jspStr = getFileString(jsp);
			WebUtil.getRequest().setAttribute("jspStr", jspStr);
			WebUtil.getRequest().setAttribute("obj", webTemplate);
			WebUtil.setSessionAttribute(BEV, webTemplate);
			return "super/WebTemplate-form";
		} else {
			throw new GlobleException("源文件路径不存在path={}", jspPath);
		}
	}

	private String getFileString(File file) throws Exception {
		FileReader in = new FileReader(file);
		BufferedReader bf=new BufferedReader(in);
		StringBuilder source = new StringBuilder();
		char[] cbuf = new char[1024];
		int len = 0;
		while ((len = bf.read(cbuf, 0, cbuf.length)) > 0) {
			source.append(new String(cbuf,0,len));
		}
		bf.close();
		in.close();
		return source.toString();
	}

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebTemplate webTemplate, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<WebTemplate> dataList = webTemplateDao.selectInPage(webTemplate, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
				webTemplateDao.selectTotalRecord(webTemplate));
		return result;
	}

	/**
	 * 删除
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date Dec 10, 2017
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(sysUsersService, keys);
	}

}